
// pages/SCM/feedback/feedback.js 
const app = getApp()
const util = require('../../../utils/util.js')
const lbbService = require('../../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page_num: 1,
    changedLocationInfo: {},
    provIndex: 0,
    cityIndex: 0,
    questionId:0,
    hospitalId:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      scrollHeight: app.getScrollHeight(111)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    let that = this
    if (that.data.changedLocationInfo&&that.data.changedLocationInfo.hospitalId) {
      that.setData({
        hospitalId: that.data.changedLocationInfo.hospitalId,
        hospitalName: that.data.changedLocationInfo.hospitalName,
        provIndex: 0,
        cityIndex: 0,
        page_num: 1,
        otherFlag:1,
        hosIndex:1,
        hosList: ['全部',that.data.changedLocationInfo.hospitalName],
        hospitalList: [{ 'id': that.data.changedLocationInfo.hospitalId, 'hospitalName': that.data.changedLocationInfo.hospitalName }]
      })
    }else {
      that.getCitys()
    }
    that.getFeedbackQuestionTypeCounts()
    that.getFeedbackList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getQueryCondition: function(){
    let that=this
    let queryCondition = {}
    if (that.data.provIndex>0){
      queryCondition.provinceId= that.data.selectorArray[0]
    }
    if (that.data.cityIndex > 0) {
        queryCondition.cityId= that.data.selectorArray[1]
    }
    if (that.data.hospitalId> 0) {
      queryCondition.hospitalId = that.data.hospitalId
    }
    if (that.data.questionId > 0) {
      queryCondition.questionId = that.data.questionId
    }
    queryCondition.pageNum = that.data.page_num
    return queryCondition
  },
  chooseHospital: function () {
    let that = this;
    wx.navigateTo({
      url: '/pages/chooseHospital/chooseHospital?hosText=' + that.data.hosText,
    })
  },

  getCitys: function () {
    let that = this
    lbbService.getCitys({}, function (res) {
      that.setData({
        propArray: res
      })
      let provList = ['全部']
      that.data.propArray.forEach(item => {
        provList.push(item.provName)
      })
      that.setData({
        provList: provList
      })
      that.setIdArray()
    })
  },
  provChange: function (e) {
    let that = this
    let index = e.detail.value
    that.setData({
      provIndex: index,
      page_num: 1,
      cityIndex: 0,
      hosIndex:0,
      changedLocationInfo:null,
    })
    that.setIdArray()
    
  },
  cityChange: function (e) {
    let that = this
    let index = e.detail.value
    that.setData({
      page_num: 1,
      cityIndex: index,
      changedLocationInfo: null,
    })
    that.setIdArray()
  },
  hosChange: function (e) {
    let that = this
    let index = e.detail.value
    let hospitalId=0
    if(index>0){
      hospitalId = that.data.hospitalList[index - 1].id
    }
    that.setData({
      page_num: 1,
      hospitalId: hospitalId,
      hosIndex: index,
      changedLocationInfo:null
    })
    that.getFeedbackList();
    that.getFeedbackQuestionTypeCounts();
  },
  setCityList: function () {
    let that = this
    let cityList = ['全部']
    if (that.data.provIndex > 0) {
      that.data.propArray[that.data.provIndex - 1].cityList.forEach(item => {
        cityList.push(item.cityName)
      })
    }
    that.setData({
      cityList: cityList
    })
    let hosList = ['全部']
    //如果是搜索医院列表过来的
    if (that.data.changedLocationInfo && that.data.changedLocationInfo.hospitalId){
      hospitalList = [{ 'id': that.data.changedLocationInfo.hospitalId, 'hospitalName': that.data.changedLocationInfo.hospitalName}]
      otherFlag=undefined;
    }
    that.data.hospitalList.forEach(item => {
      hosList.push(item.hospitalName)
    })
    that.setData({
      hosList: hosList
    })
  },
  getHospitalList: function () {
    let that = this
    let selectorArray = that.data.selectorArray
    lbbService.getHospitalList({
      array: selectorArray
    }, function (res) {
      that.setData({
        hospitalList: res
      })
      that.setCityList()
    })
  },
  setIdArray: function () {
    let that = this
    let provId = 0
    if (that.data.provIndex > 0) {
      provId = that.data.propArray[that.data.provIndex - 1].id
    }
    let cityId = 0
    if (that.data.cityIndex > 0) {
      if (that.data.provIndex > 0) {
        cityId = that.data.propArray[that.data.provIndex - 1].cityList[that.data.cityIndex - 1].cityId
      } else {
        cityId = that.data.propArray[that.data.provIndex].cityList[that.data.cityIndex - 1].cityId
      }
    }
    let selectorArray = [provId, cityId, 0]
    that.setData({
      selectorArray: selectorArray,
      hospitalId: '',
      hospitalName: '',
    })
    that.getHospitalList()
    that.getFeedbackList()
    that.getFeedbackQuestionTypeCounts();
  },

  getMore: function () {
    let that = this
    that.data.page_num += 1
    that.getFeedbackList()
  },

  typeChange: function (e) {
    let that = this
    let id = e.detail.value
    console.log(e)
    that.setData({
      questionId: id,
      page_num: 1
    })
    let quetionTypes = that.data.quetionTypes
    quetionTypes.forEach(item => {
      item.checked = false
      if (item.questionId == that.data.questionId) {
        item.checked = true
      }
    })
    that.setData({
      quetionTypes: quetionTypes
    })
    that.getFeedbackList()
  },

  inputHosName:function(e){
    console.log(e)
    let hosText = e.detail.value;
    this.setData({
      hosText: hosText
    })
  },

  getFeedbackQuestionTypeCounts: function () {
    let that = this
    lbbService.getFeedbackQuestionTypeCounts(that.getQueryCondition(), function (res) {
      let quetionTypes = that.data.quetionTypes;
      if (quetionTypes){
        quetionTypes.forEach(item => {
          try {
            res.data.forEach(item1 => {
              if (item1.questionId == item.questionId) {
                item.num = item1.num;
                throw new Error();
              }
            })
          } catch (e) {  }
        })
        that.setData({
          quetionTypes: quetionTypes
        })
      }else {
        that.setData({
          quetionTypes: res.data
        })
      }
    })

  },

  getFeedbackList: function () {
    let that = this
    let qc = that.getQueryCondition();
    console.log("qcqcqcqcq",qc)
    lbbService.getFeedbackList(qc, function (res) {
      if (that.data.page_num != 1) {
        that.setData({
          feedbackList: that.data.feedbackList.concat(res.data)
        })
      } else {
        that.setData({
          feedbackList: res.data,
        })

      }
    })
  },
})